var shareDAO = require('../models/shareDAO')
var formidable = require('formidable')
var path = require('path')
var shareController = {
    //分享圈
    shareContent: function (req, res) {
        shareDAO.shareContent(function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '用户分享失败' })
            } else {
                res.json({ code: 200, data: results, msg: '用户分享成功!' })

            }
        })
    },

    //已发布内容
    publish: function (req, res) {
        // console.log("hahah:"+req.user)
        shareDAO.publish(req.user, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '获取用户发布内容失败' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '用户发布内容成功!' })
                } else {
                    res.json({ code: 200, msg: '获取用户发布内容失败' })
                }
            }
        })
    },

    getshareuser: function (req, res) {
        console.log(req.params.upmenuId)
        var upmenuId = req.params.upmenuId
        shareDAO.shareuser(upmenuId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '获取用户信息失败' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '获取用户信息成功!' })
                } else {
                    res.json({ code: 200, msg: '获取用户信息失败' })
                }
            }
        })
    },

    //分享圈详情
    sharedetail: function (req, res) {
        var upmenuId = req.params.upmenuId
        shareDAO.sharedetail(upmenuId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '用户获取分享圈详情失败' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '用户获取分享圈详情成功!' })
                } else {
                    res.json({ code: 200, msg: '用户获取分享圈详情失败' })
                }
            }
        })
    },

    //已发布内容的详情
    publishdetail: function (req, res) {
        var upmenuId = req.params.upmenuId
        shareDAO.publishdetail(upmenuId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '用户获取发布内容详情失败' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '用户获取发布内容详情成功!' })
                } else {
                    res.json({ code: 200, msg: '用户获取发布内容详情失败' })
                }
            }
        })
    },
    //上传菜谱 （坏）
    //  upmenu: function (req, res) {
    //     var upmenuTitle = req.body.upmenuTitle
    //     var finishPic = req.body.finishPic
    //     var story = req.body.story
    //     var touserId = req.body.touserId
    //     shareDAO.upmenu({ upmenuTitle: upmenuTitle, finishPic: finishPic, story: story, touserId: touserId }, function (err, results) {
    //         if (err) {
    //             res.json({ code: 500, data: err.message, msg: '用户上传菜谱失败' })
    //         } else {
    //                var stepId = req.body.stepId
    //                var stepPic = req.body.stepPic
    //                var stepMake = req.body.stepMake
    //                var toupmenuId = req.body.toupmenuId
    //             // res.json({ code: 200, data: results, msg: '用户上传菜谱成功!' })
    //                shareDAO.upmenustep({stepId:stepId,stepPic:stepPic,stepMake:stepMake,toupmenuId:toupmenuId},function(err,result){
    //                    if(err){
    //                        res.json({code:500, msg:'用户上传菜谱失败'})
    //                    }else{
    //                     console.log('用户上传菜谱成功')
    //                     res.json({ code:200, data:results,msg:'用户上传菜谱成功'})
    //                     console.log(results)
    //                    }
    //                })
    //         }
    //     })
    // },
    //测试一半代码（成品图）
    upmenu: function (req, res) {
        var form = new formidable.IncomingForm();   //创建上传表单对象
        form.uploadDir = path.join(__dirname, '..', '/public/upload');   //设置上传文件的路径
        form.keepExtensions = true   //设置保留上传文件的扩展名
        form.parse(req, function (err, fields, files) {   //实现上传文件和字段的转换
            if (err) {
                res.send('文件上传错误！')
            }
            //fields是上传的表单字段数组，files是上传的文件列表
            //保存图片路径到数据库
            //1.获取当前用户编号
            console.log(fields)
            //2.获取当前用户的图片名称
            console.log(files)
            var upmenuTitle = fields.upmenuTitle
            var finishPic = path.parse(files.finishPic.path).base
            var story = fields.story
            var touserId = fields.touserId
            shareDAO.upmenu({ upmenuTitle: upmenuTitle, finishPic: finishPic, story: story, touserId: touserId }, function (err, result) {
                if (err) {
                    res.json({ code: 500, data: err, msg: '用户上传菜谱失败' })
                } else {
                    res.json({ code: 200, data: result, msg: '用户上传菜谱成功!' })
                }
            })
        })
    },
    //测试一半代码（步骤图）
    upmenusteps: function (req, res) {
        console.log('传进来的req:', req)
        var form = new formidable.IncomingForm();   //创建上传表单对象
        form.uploadDir = path.join(__dirname, '..', '/public/upload');   //设置上传文件的路径
        form.keepExtensions = true   //设置保留上传文件的扩展名
        form.parse(req, function (err, fields, files) {   //实现上传文件和字段的转换
            if (err) {
                res.send('文件上传错误！')
            }
            //fields是上传的表单字段数组，files是上传的文件列表
            //保存图片路径到数据库
            //1.获取当前用户编号
            console.log(fields)
            //2.获取当前用户的图片名称
            console.log(files)
            var stepPic1 = path.parse(files.img_0.path).base
            var stepMake1 = fields.stepMake1
            var stepPic2 = path.parse(files.img_1.path).base
            var stepMake2 = fields.stepMake2
            var stepPic3 = path.parse(files.img_2.path).base
            var stepMake3 = fields.stepMake3
            var stepPic4 = path.parse(files.img_3.path).base
            var stepMake4 = fields.stepMake4
            var touserId = fields.touserId
            console.log('字段：', stepPic1)
            shareDAO.upmenustep({ stepPic1: stepPic1, stepMake1: stepMake1, stepPic2: stepPic2, stepMake2: stepMake2, stepPic3: stepPic3, stepMake3: stepMake3, stepPic4: stepPic4, stepMake4: stepMake4, touserId: touserId }, function (err, result) {
                if (err) {
                    res.json({ code: 500, data: err, msg: '用户上传菜谱失败' })
                } else {
                    console.log('用户上传菜谱成功')
                    res.json({ code: 200, data: result, msg: '用户上传菜谱成功' })
                    console.log(result)
                }
            })

        })
    },

    //新上传菜谱
    // upmenu: function (req, res) {
    //     var form = new formidable.IncomingForm();   //创建上传表单对象
    //     form.uploadDir = path.join(__dirname, '..', '/public/upload');   //设置上传文件的路径
    //     form.keepExtensions = true   //设置保留上传文件的扩展名
    //     form.parse(req, function (err, fields, files) {   //实现上传文件和字段的转换
    //         if (err) {
    //             res.send('文件上传错误！')
    //         }
    //         //fields是上传的表单字段数组，files是上传的文件列表
    //         //保存图片路径到数据库
    //         //1.获取当前用户编号
    //         console.log(fields)
    //         //2.获取当前用户的图片名称
    //         console.log(files)
    //         var upmenuTitle = fields.upmenuTitle
    //         var finishPic = path.parse(files.finishPic.path).base
    //         var story = fields.story
    //         var touserId = fields.touserId
    //         shareDAO.upmenu({ upmenuTitle: upmenuTitle, finishPic: finishPic, story: story, touserId: touserId }, function (err, result) {
    //             if (err) {
    //                 res.json({ code: 500, data: err, msg: '用户上传菜谱失败' })
    //             } else {
    //                 var step = {
    //                     stepPic1:path.parse(files.stepPic1.path).base,
    //                     stepMake1: fields.stepMake1,
    //                     stepPic2:path.parse(files.stepPic2.path).base,
    //                     stepMake2: fields.stepMake2,
    //                     stepPic3:path.parse(files.stepPic3.path).base,
    //                     stepMake3:fields.stepMake3,
    //                     stepPic4:path.parse(files.stepPic4.path).base,
    //                     stepMake4: fields.stepMake4,
    //                     touserId:fields.touserId
    //                 }

    //                 shareDAO.upmenustep(step, function (err, result) {
    //                     if (err) {
    //                         res.json({ code: 500,data:err, msg: '用户上传菜谱失败' })
    //                     } else {
    //                         console.log('用户上传菜谱成功')
    //                         res.json({ code: 200, data: result, msg: '用户上传菜谱成功' })
    //                         console.log(results)
    //                     }
    //                 })
    //                 res.json({ code: 200, data: results, msg: '用户上传菜谱成功!' })
    //             }
    //         })
    //     })
    // },

    //删除已发布菜谱
    personal: function (req, res) {
        var upmenuId = req.params.upmenuId
        shareDAO.personalConment(upmenuId, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '删除已上传的菜谱失败' })
            } else {
                shareDAO.deletepersonal(upmenuId, function (err, results) {
                    if (err) {
                        res.json({ code: 500, msg: '删除失败' })
                    } else {
                        console.log('成功删除上传的菜谱')
                        res.json({ code: 200, data: results, msg: '删除成功' })
                        console.log(results)
                    }
                })
            }
        })
    },

        //查看点赞表是否有点赞
        selSharelike: function (req, res) {
            shareDAO.selsharelike(req.user, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '该用户查询数据库错误！' })
                } else {
                    if (results.length >= 0) {
                        var upmenuId = req.body.upmenuId
                        if (results.length != 0) {
                            for (var i = 0; i < results.length; i++) {
                                if (upmenuId == results[i].toupmenuId) {
                                    res.json({ code: 200, data: 0, msg: '该用户点赞过这个套餐！' });
                                }
                            }
                        }
                    } else {
                        res.json({ code: 200, data: results, msg: '获取数据库点赞记录失败！' })
                    }
    
                }
            })
        },
    //增加分享圈点赞数
    addUserlike: function (req, res) {
        // var userId = req.body.userId
        shareDAO.selsharelike(req.user, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询点赞数失败' })
            } else {
                if (results.length >= 0) {
                    var upmenuId = req.body.upmenuId
                    var fs = 0;
                    var flag = 1;
                    if (results.length != 0) {
                        for (var i = 0; i < results.length; i++) {
                            if (upmenuId == results[i].toupmenuId) {
                                res.json({ code: 200, data: 0, msg: '该用户已经点过赞' })
                                flag = 0
                                return

                            }
                        }
                    } else if (results.length == 0) {
                        fs = 1;
                        console.log(fs)
                    }
                    if (flag == 1 || fs == 1) {
                        // console.log(userId)
                        console.log(req.user.phone)
                        console.log(upmenuId)
                        shareDAO.insertLike({ upmenuId: upmenuId, user: req.user }, function (err, resinsert) {
                            if (err) {
                                res.json({ code: 500, msg: '在数据库中插入一条点赞记录失败！' })
                            } else {
                                if (resinsert.affectedRows == 1) {
                                    shareDAO.addUserlike(upmenuId, function (err, resadd) {
                                        if (err) {
                                            res.json({ code: 500, msg: '数据库点赞增加错误' })
                                        } else {
                                            if (resadd.affectedRows == 1) {
                                                res.json({ code: 200, data: resadd.affectedRows, msg: '点赞成功' })
                                            } else {
                                                res.json({ code: 200, data: resadd.affectedRows, msg: '点赞失败' })
                                            }
                                        }
                                    })
                                } else {
                                    res.json({ code: 200, data: resinsert.affectedRows, msg: '插入一条点赞记录失败' })
                                }
                            }
                        })
                    }
                } else {
                    res.json({ code: 200, data: results, msg: '该用户没有点赞记录' })
                }
            }
        })
    },
    //取消分析圈点赞数
    celUserlike: function (req, res) {
        // var userId = req.body.userId
        shareDAO.selsharelike(req.user, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询数据库失败' })
            } else {
                if (results.length != 0) {
                    var upmenuId = req.body.upmenuId
                    var flag = 1
                    for (var i = 0; i < results.length; i++) {
                        if (upmenuId == results[i].toupmenuId) {
                            shareDAO.delLike({ upmenuId: upmenuId, user: req.user }, function (err, resdelete) {
                                if (err) {
                                    console.log(userId)
                                    console.log(upmenuId)
                                    res.json({ code: 500, msg: '在数据库中删除点赞记录失败！' })
                                } else {
                                    if (resdelete.affectedRows == 1) {

                                        shareDAO.celUserlike(upmenuId, function (err, resdel) {
                                            if (err) {
                                                res.json({ code: 500, msg: '数据库点赞取消失败' })
                                            } else {
                                                if (resdel.affectedRows == 1) {
                                                    res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞成功！' })
                                                } else {
                                                    res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞失败！' })
                                                }
                                            }
                                        })
                                    } else {
                                        res.json({ code: 200, data: resdelete.affectedRows, msg: '删除点赞记录失败！' })
                                    }

                                }
                            })
                            flag = 0
                        }
                    }
                    if (flag == 1) {
                        res.json({ code: 200, data: 0, msg: '该用没有点赞过这个套餐！' });
                    }
                } else {
                    res.json({ code: 200, data: results, msg: '取消点赞数成功' })
                }
            }

        })
    }
}
module.exports = shareController                                                                                                                                                                                                                                                   